package com.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiku.common.ResultJson;
import com.tiku.entity.ChapterInfo;
import com.tiku.entity.ChapterSubInfo;
import com.tiku.entity.QuestionTable;
import com.tiku.entity.UserQuestionLog;
import com.tiku.mapper.ChapterInfoMapper;
import com.tiku.mapper.ChapterSubInfoMapper;
import com.tiku.mapper.UserQuestionLogMapper;
import com.tiku.service.QuestionTableService;
import com.tiku.mapper.QuestionTableMapper;
import com.tiku.service.UserCollectionService;
import com.tiku.utils.RedisCache;
import com.tiku.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 */
@Service
public class QuestionTableServiceImpl extends ServiceImpl<QuestionTableMapper, QuestionTable>
    implements QuestionTableService{


    @Autowired
    private QuestionTableMapper questionTableMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private UserQuestionLogMapper questionLogMapper;

    @Autowired
    private ChapterSubInfoMapper chapterSubInfoMapper;

    @Autowired
    private ChapterInfoMapper chapterInfoMapper;

    @Autowired
    private UserCollectionService userCollectionService;
    @Override
    public ResultJson getQuestionList(String id,String specification,String menuId) {
        List<QuestionTable> questionTables = questionTableMapper.selectList(new QueryWrapper<QuestionTable>().lambda()
                .eq(QuestionTable::getChapterId, id)
                .orderByAsc(QuestionTable::getQuestionTypeType));
        String userId = ThreadLocalUtil.getThreadLocal();

        Object cacheObject = redisCache.getCacheObject("question:" + userId + id);
        if (cacheObject== null) {
            cacheObject=0;
        }
        for (QuestionTable questionTable : questionTables) {
            String questionId = questionTable.getId();
            ChapterSubInfo chapterSubInfo = chapterSubInfoMapper.selectById(questionTable.getChapterId());
            Boolean aBoolean = userCollectionService.selectCollectionDO(questionId);
            questionTable.setCollectFlagDO(aBoolean);
            String chapterId;
            String id1;

            if (Objects.isNull(chapterSubInfo)){
                ChapterInfo chapterInfo = chapterInfoMapper.selectById(questionTable.getChapterId());
                chapterId = chapterInfo.getId();
                id1 = chapterInfo.getId();
            }else {
                 chapterId = chapterSubInfo.getChapterId();
                 id1 = chapterSubInfo.getId();
            }

            UserQuestionLog questionLog =getQuestionLog(userId,questionId,chapterId,id1,specification,menuId);



            if (questionLog==null) {
                questionTable.setUserAnswer(null);
                questionTable.setRightFlag(null);
            }else {
                questionTable.setUserAnswer(questionLog.getUseranswer());
                questionTable.setRightFlag(questionLog.getRightflag());
            }

        }

        Map<String,Object> map=new HashMap<>();
        map.put("index",cacheObject);
        map.put("questions", questionTables);
        return ResultJson.success(map);
    }
    //key : questionLog: + 用户id + 类目ID + 菜单ID+ 章节ID + 子章节ID + 题目ID
    private UserQuestionLog getQuestionLog(String userId, String questionId, String chapterId, String chapterSubId,String specification,String menuId) {

        String chapterText=chapterSubId;
        String idTmp=chapterSubId;
        while (true){
            ChapterSubInfo chapterSubInfo = chapterSubInfoMapper.selectById(idTmp);
            if (Objects.isNull(chapterSubInfo)){

                break;
            }else {
                idTmp=chapterSubInfo.getChapterId();
                chapterText=idTmp+chapterText;
            }
        }

        UserQuestionLog questionLog = redisCache.getCacheObject("questionLog:" + userId +specification+ menuId+chapterText+questionId);
        if (questionLog==null){
            UserQuestionLog userQuestionLog = questionLogMapper.selectOne(new LambdaQueryWrapper<UserQuestionLog>().
                    eq(UserQuestionLog::getUserId, userId).
                    eq(UserQuestionLog::getQuestionId, questionId));
            if (userQuestionLog!=null){
                questionLog = userQuestionLog;
            }
        }
        return questionLog;
    }

    @Override
    public ResultJson rollback(String id,Integer index) {
        String threadLocal = ThreadLocalUtil.getThreadLocal();
        redisCache.setCacheObject("question:"+threadLocal+id,index);
        return ResultJson.success(null);
    }
}




